package srst.ai;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import srst.ai.NlpirSegmentUtil.CLibrary;

/** 
* @author  作者 : YUHU YUAN
* @date 创建时间：2017年5月14日 下午3:09:43 
* @version 1.0  
*/

public class SegmentWordUtil {
	
	private static Logger logger = Logger.getLogger(SegmentWordUtil.class.getName());
	
	
//	public String generateSegmentedWordResultByInitSeed(String text, List<String> keywords){
//		String argu = System.getProperty("user.dir");
//		int charset_type = 1;
//		int init_flag = CLibrary.Instance.NLPIR_Init(argu, charset_type, "0");
//		String nativeBytes = null;
//		if (0 == init_flag) {
//			nativeBytes = CLibrary.Instance.NLPIR_GetLastErrorMsg();
//			logger.info("初始化失败！fail reason is "+nativeBytes);
//		}
//		String sInput = text;
//		try {
//			for(String word : keywords){
//				CLibrary.Instance.NLPIR_AddUserWord(word);
//			}
//			nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sInput, 1);
//			logger.info("增加用户词典后分词结果为:" + nativeBytes);
//			sInput = nativeBytes.replaceAll("(/[a-z,0-9]+ )", " ");
//			CLibrary.Instance.NLPIR_Exit();
//		} catch (Exception ex) {
//			// TODO Auto-generated catch block
//			ex.printStackTrace();
//		}
//		return sInput;
//	}
//	
//	public void write2File(String text){
//		BufferedWriter writer = FileUtil.getWriter(FilePathUtil.SEGMENT_RESUME_PATH);
//		try {
//			writer.write(text);
//			writer.newLine();
//			writer.flush();
//		
//		if(writer != null){
//			writer.close();
//		}
//		
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
	
	public static List<String> generateSegmentedWordResultByInitSeed(List<String> texts, List<String> keywords){
		String argu = System.getProperty("user.dir");
		int charset_type = 1;
		int init_flag = CLibrary.Instance.NLPIR_Init(argu, charset_type, "0");
		String nativeBytes = null;
		if (0 == init_flag) {
			nativeBytes = CLibrary.Instance.NLPIR_GetLastErrorMsg();
			logger.info("初始化失败！fail reason is "+nativeBytes);
		}
		
		List<String> segmentedText = new ArrayList<>();
		
		try {
			for(String word : keywords){
				CLibrary.Instance.NLPIR_AddUserWord(word);
			}
			for(String  sInput: texts){
				nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(sInput, 1);
				Logger.getLogger("yuan_data").info("增加用户词典后分词结果为:" + nativeBytes);
				sInput = nativeBytes.replaceAll("(/[a-z,0-9]+ )", " ");
				segmentedText.add(sInput);
			}
			CLibrary.Instance.NLPIR_Exit();
		} catch (Exception ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
		return segmentedText;
	}
	
	
	
	
//	public List<String> keywordsList() throws IOException{
//		return KeywordService.getKeywordList(FilePathUtil.KEYWORDS_PATH);
//	}
//	
//	public String pdfText() throws IOException{
//		return PDFReaderUtil.getTextFromPDF(FilePathUtil.TEXT_PATH).replaceAll("[(\\s)|( )|( )]+", " ");
//	}
	
//	public static void main(String[] args) throws IOException{
//		SegmentWordUtil segmentWordServive = new SegmentWordUtil();
//		String text = segmentWordServive.pdfText();
//		System.out.println(text);
//		List<String> keywords = segmentWordServive.keywordsList();
//		String segmentedText = segmentWordServive.generateSegmentedWordResultByInitSeed(text, keywords);
//		System.out.println("==============");
//		System.out.println(segmentedText);
//		segmentWordServive.write2File(segmentedText);
//		
//		
//	}
	
	

}























